package com.lc.boot.context;

import java.util.Map;
import java.util.WeakHashMap;

/**
 * @author liuc
 * @date 2021/5/2
 * @description 封装命令行传入参数
 **/
public class CommandLineProperties {

    //存储命令行传入参数
    private Map<String,String> map = new WeakHashMap<>();

    public CommandLineProperties(Object... args){
        if(args != null &&  args.length != 0){
            for (Object arg : args) {
                String s = (String) arg;
                String[] split = s.split("=");
                if(split.length < 2){
                    continue;
                }
                map.put(split[0],split[1]);
            }
        }
    }

    public String getString(String property){
        return map.get(property);
    }

    public Integer getInteger(String property, Integer dft){
        String var = getString(property);
        if(var == null){
            return dft;
        }
        try {
            return Integer.parseInt(var);
        } catch (NumberFormatException e) {
            //
            return dft;
        }
    }


}
